package com.qf.java2007.interceptor;

import com.qf.java2007.pojo.Admin;
import com.qf.java2007.pojo.ReaderCard;
import org.springframework.http.HttpRequest;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 权限拦截
 * @Author AzureSky_X
 * @Date 2021/3/13 14:50
 * @Version 1.0
 */
public class PermissionInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //获取请求的url
        StringBuffer url=request.getRequestURL();
        //除了登录相关的jsp页面可以公开访问，其他请求需要拦截控制
        if (url.indexOf("/login")>0||url.indexOf("/login/login")>0){
            return true;
        }

        //获取session
        HttpSession session = request.getSession();
        //获取登录信息
        Admin admin= (Admin) session.getAttribute("admin");
        ReaderCard readercard= (ReaderCard) session.getAttribute("readercard");
        //判断session里是不是有登录信息
        if(admin!=null || readercard!=null) {
            return true;
        }
        request.setAttribute("msg", "你还没有登录，请先登录！");
        //request.getRequestDispatcher("/").forward(request, response);
        response.sendRedirect("/");


        return false;







    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
